package com.itheima.Advice;

import com.itheima.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//切面
@Aspect
//被扫描
@Component
public class TransferAdvice2 {
    //注入
    @Autowired
    private LogService logService;
    //切点
    @Pointcut("execution(* com.itheima.service.AccountService.transfer(..))")
    public void pt(){}

    //返回通知
    @AfterReturning(value = "pt()",returning = "object")
    public void returning(JoinPoint joinPoint,Object object){
        //1.获取参数
        Object[] args = joinPoint.getArgs();
        String out = args[0].toString();
        String in = args[1].toString();
        double money = Double.parseDouble(args[2].toString());

        //返回通知的记录--成功。注入logservice
        String info="转账成功！！";
        System.out.println(info);
        logService.log(out,in,money);
    }
    //异常通知
    @AfterThrowing(value = "pt()",throwing = "t")
    public void throwing(JoinPoint joinPoint,Throwable t){
        //1.获取参数
        Object[] args = joinPoint.getArgs();
        String out = args[0].toString();
        String in = args[1].toString();
        double money = Double.parseDouble(args[2].toString());

        //2.异常通知--失败原因
        String info="转账失败！！"+t.getMessage();
        System.out.println(info);
        logService.log(out,in,money);

    }//此代码和TransferAdvice类中的注释的方法一致
}
